package com.zyd.module.admin.controller;


import com.zyd.common.util.CoreConst;
import com.zyd.common.util.ResultUtil;
import com.zyd.module.admin.service.ProductInfosService;
import com.zyd.module.admin.vo.base.ResponseVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.ApiOperation;


import java.util.List;


import com.zyd.module.admin.model.ProductType;
import com.zyd.module.admin.service.ProductTypeService;


/**
 * 
 *
 * @author hui
 * @email *****@mail.com
 * @date 2022-04-04 00:17:38
 */
@Controller
@RequestMapping("/types")
public class ProductTypeController {
    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductInfosService productInfosService;



    /**
    * 跳转到页面
    */
    @PostMapping("/list")
    @ResponseBody
    public List<ProductType> loadCategory(boolean isFistLevel) {
        ProductType typeEntity=new ProductType();
        typeEntity.setIsDelete(CoreConst.DELETE_NO);
        if (isFistLevel) {
            typeEntity.setFid(CoreConst.TOP_MENU_ID);
        }
        return productTypeService.selectTypeEntityList(typeEntity);
    }

    @ApiOperation(value = "新增")
    @GetMapping("/add")
    public String add() {
        return CoreConst.ADMIN_PREFIX + "types/form";
    }

    @PostMapping("/add")
    @ResponseBody
    @CacheEvict(value = "typeInfo", allEntries = true)
    public ResponseVo add(ProductType typeEntity) {
//        if (!CoreConst.TOP_MENU_ID.equals(typeEntity.getFid())) {
//
//            List<ProductInfosEntity> productInfosList = productInfosService.selectProductInfosByTypeId(typeEntity.getFid());
//            if (!ListUtils.isEmpty(productInfosList)) {
//                return ResultUtil.error("添加失败，父级分类下存在产品");
//            }
//        }
        typeEntity.setIsDelete(CoreConst.DELETE_NO);
        boolean i = productTypeService.save(typeEntity);
        if (i) {
            return ResultUtil.success("新增分类成功");
        } else {
            return ResultUtil.error("新增分类失败");
        }
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        ProductType typeEntity= productTypeService.selectById(id);
        model.addAttribute("typeInfo", typeEntity);
        return CoreConst.ADMIN_PREFIX + "types/form";
    }

    @PostMapping("/edit")
    @ResponseBody
    @CacheEvict(value = "typeInfo", allEntries = true)
    public ResponseVo edit(ProductType typeEntity) {
        boolean i = productTypeService.updateById(typeEntity);
        if (i) {
            return ResultUtil.success("编辑分类成功");
        } else {
            return ResultUtil.error("编辑分类失败");
        }
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        if (CollectionUtils.isNotEmpty(productTypeService.selectByFid(id))) {
            return ResultUtil.error("该分类下存在子分类！");
        }
        return deleteBatch(new Integer[]{id});
    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = productTypeService.deleteBatch(ids);
        if (i > 0) {
            return ResultUtil.success("删除分类成功");
        } else {
            return ResultUtil.error("删除分类失败");
        }
    }


}
